package top.italks.camera;

import static android.hardware.Camera.getNumberOfCameras;

import android.graphics.SurfaceTexture;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.Gravity;
import android.view.TextureView;
import android.view.TextureView.SurfaceTextureListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import androidx.appcompat.app.AppCompatActivity;
import java.io.IOException;
import top.italks.camera.MainActivity.CircleViewOutlineProvider;

public class CameraActivity extends AppCompatActivity implements SurfaceTextureListener {

    private TextureView textureView;
    private Camera mCamera;
    private ImageView minphoto;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera);
        minphoto = (ImageView) findViewById(R.id.minphoto);
        textureView = (TextureView) findViewById(R.id.textureView);
        textureView.setSurfaceTextureListener(this);


    }

    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
        int num = getNumberOfCameras();
        mCamera = Camera.open(0);
        Camera.Size previewSize = mCamera.getParameters().getPreviewSize();
        textureView.setLayoutParams(new LinearLayout.LayoutParams(
            previewSize.width, previewSize.height, Gravity.CENTER));
        try {
            mCamera.setPreviewTexture(surface);
        } catch (IOException t) {
        }
        mCamera.startPreview();
        textureView.setAlpha(1.0f);
        textureView.setScaleX(-1);
        textureView.setOutlineProvider(new CircleViewOutlineProvider());
        textureView.setClipToOutline(true);
//        textureView.setRotation(90.0f);
    }

    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {

    }

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
        mCamera.stopPreview();
        mCamera.release();
        return true;
    }

    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture surface) {

    }
}